Vue.component('add', {
    data: function () {
        return {
            ma: [
                { name: '正码一', value: '', index:0},
                { name: '正码二', value: '', index:1 },
                { name: '正码三', value: '', index:2 },
                { name: '正码四', value: '', index:3 },
                { name: '正码五', value: '', index:4 },
                { name: '正码六', value: '', index:5 },
                { name: '特码', value: '', index:6 }
            ],
            res:[0,0,0,0,0,0],
            time:'',
            qi:''
        }
    },
    template:'#add-page-template',
    props:['params'],
    methods:{
        save:function () {
            let data = {time:parseInt(this.time), code1:'', code2:'', code3:'', code4:'', code5:'', code6:'', tcode:'', qi:parseInt(this.qi)};
            if (!data.time) {
                layer.msg('请选择开奖日期');
                return false;
            }
            if (!data.qi) {
                layer.msg('请输入期号，且期号只能为数字');
                return false;
            }

            for (let i in this.res) {
                let tmp = parseInt(this.res[i]);
                if (!tmp) {
                    layer.msg('请填写' + this.ma[i].name);
                    return false;
                }
                if (tmp > 49 || tmp < 1) {
                    layer.msg(this.ma[i].name + '只能是1-49之间的数字');
                    return false;
                }

                if (i < 6) {
                    data['code' + (parseInt(i) + 1)] = tmp;
                } else {
                    data.tcode = tmp;
                }
            }

            let _self = this;
            if (_self.params) {
                database.update('code', {time:_self.params}, data, function (err, res) {
                    if (!err) {
                        layer.msg('保存成功');
                    } else {
                        layer.msg('保存失败：' + err);
                    }
                });
            } else {
                database.add('code', data, function (err, res) {
                    if (!err) {
                        _self.res = [0,0,0,0,0,0];
                        let ma = _self.ma;
                        _self.ma = [];
                        layer.msg('保存成功');
                        _self.ma = ma;
                    } else {
                        layer.msg('保存失败：' + err);
                    }
                });
            }

        },
        setRes:function (res) {
            this.res[res.index] = res.value;
        },
        initDate:function () {
            laydate.render({
                elem: '#index-add-date'
                ,format: 'yyyyMMdd'
                ,min: '2019-01-01'
                ,max: '2019-12-31'
                ,done: this.changeDate
            });
        },
        changeDate:function(value, date, endDate){
            this.time = value;
        },
        changeTab:function () {
            this.$emit('changeroute', 'query')
        },
        getData:function (time) {
            let _self = this;
            console.log('time:', time);
            database.getRow('code', {time: time}, function (res, err) {
                if (err) {
                    layer.msg('数据不存在');
                    _self.changeTab();
                    return false;
                }

                _self.res = [res.code1, res.code2, res.code3, res.code4, res.code5, res.code6, res.tcode];
                _self.time = time;
                _self.qi = res.qi;
                _self.ma = [
                    { name: '正码一', value: res.code1, index:0},
                    { name: '正码二', value: res.code2, index:1 },
                    { name: '正码三', value: res.code3, index:2 },
                    { name: '正码四', value: res.code4, index:3 },
                    { name: '正码五', value: res.code5, index:4 },
                    { name: '正码六', value: res.code6, index:5 },
                    { name: '特码', value: res.tcode, index:6 }
                ]
            });
        },
        del:function (time) {
            let _self = this;
            layer.confirm('确认删除？', function () {
                database.delete('code', {time:_self.time}, function (err, res) {
                    layer.closeAll();
                    if (err) {
                        layer.msg(err);
                        return false;
                    }
                    layer.msg('删除成功');
                    _self.changeTab();
                });
            });
        }
    },
    mounted:function () {
        this.initDate();
    },
    created:function () {
        if (this.params) {
            this.getData(this.params);
        }
        console.log('params:',this.params);
    }
});


/**生肖转换组件**/
Vue.component('zodiac', {
    props:['item'],
    template:'#zodiac-page-template',
    data:function () {
        return {
            zodiacs:zodiac.config.zodiac,
            code:-1,
            currZodiac:{
                pos:-1,
                name:'',
                letter:'',
                wuxing:'',
                jixiong:'',
                jiaye:'',
                tiandi:'',
                yinyang:'',
                danshuang:'',
                heibai:'',
                nannv:'',
                yanse:'',
                jijie:'',
                fangxiang:'',
                qqsh:'',
                bo:'',
                he:'',
                daxiao:'',
            }
        }
    },
    created:function () {
        console.log(this.item)
    },
    mounted:function () {

    },
    computed:{
        currZodiacsName:function () {
            if (this.currZodiac >= 0){
                let zodiacArr =  zodiac.getAgeZodiac(this.code);
                return zodiacArr.name
            }
            return '';
        }
    },
    methods:{
        setCurrZodiac:function () {
            let tmp = parseInt(this.item.value);
            if (!tmp) {
                layer.msg('请输入数字');
                this.currZodiac = {
                    pos:-1,
                    name:'',
                    letter:'',
                    wuxing:'',
                    jixiong:'',
                    jiaye:'',
                    tiandi:'',
                    yinyang:'',
                    danshuang:'',
                    heibai:'',
                    nannv:'',
                    yanse:'',
                    jijie:'',
                    fangxiang:'',
                    qqsh:'',
                    bo:'',
                    he:'',
                    daxiao:'',
                };
                return false;
            }
            if (tmp < 1 || tmp > 49) {
                layer.msg('请输入 1-49 的数字');
                this.currZodiac = {
                    pos:-1,
                    name:'',
                    letter:'',
                    wuxing:'',
                    jixiong:'',
                    jiaye:'',
                    tiandi:'',
                    yinyang:'',
                    danshuang:'',
                    heibai:'',
                    nannv:'',
                    yanse:'',
                    jijie:'',
                    fangxiang:'',
                    qqsh:'',
                    bo:'',
                    he:'',
                    daxiao:'',
                };
                return false;
            }
            this.code = tmp;
            this.currZodiac = zodiac.getCodeZodiac(this.code);
            return true;
        },
        resetCurrZodiac:function () {
            this.code = -1;
        }
    },
    watch:{
        'item.value':function () {
            this.$emit('change', this.item);
            return this.setCurrZodiac();
        }
    }
});